package com.shq.ex1.bus.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.CursorAdapter;
import android.widget.TextView;

import com.shq.ex1.R;

public class ZZhanActivity extends Activity implements TextWatcher{
	
	private AutoCompleteTextView zhan01 = null;
	private AutoCompleteTextView zhan02 = null;
	private Button zzhanBtn = null;
	private SQLiteDatabase db = null;
	private String zhanStr01 = null;
	private String zhanStr02 = null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.zhanzhan);
		zhan01 = (AutoCompleteTextView) findViewById(R.id.zhan01);
		zhan02 = (AutoCompleteTextView) findViewById(R.id.zhan02);
		zzhanBtn = (Button) findViewById(R.id.zzhanBtn);
		Intent intent = getIntent();
		final String databasePath = intent.getStringExtra("databasePath");
		db = SQLiteDatabase.openOrCreateDatabase(databasePath, null);
		zzhanBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if (zhan01 != null && zhan01.getText()!= null&&zhan02 != null && zhan02.getText()!= null) {
					zhanStr01 = zhan01.getText().toString();
					zhanStr02 = zhan02.getText().toString();
					Intent intent = new Intent();
					intent.putExtra("databasePath", databasePath);
					intent.putExtra("zhandian01", zhanStr01);
					intent.putExtra("zhandian02", zhanStr02);
					intent.setClass(ZZhanActivity.this, ZZhanResultActivity.class);
					ZZhanActivity.this.startActivity(intent);
				}
			}
		});
		
		
		zhan01.addTextChangedListener(this);
		zhan02.addTextChangedListener(this);
	}

	@Override
	public void afterTextChanged(Editable arg0) {
		Cursor cursor = db.rawQuery("select distinct zhan as _id from cnbus where zhan like ?", new String[]{"%" + arg0.toString() +"%"});
		ZhanAdapter adapter = new ZhanAdapter(ZZhanActivity.this, cursor, true);
		zhan01.setAdapter(adapter);
		zhan02.setAdapter(adapter);
	}

	@Override
	public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
			int arg3) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
		// TODO Auto-generated method stub

	}
	public class ZhanAdapter extends CursorAdapter{

		private LayoutInflater layoutInflater;
		
		public CharSequence convertToString(Cursor cursor)
		{
			return cursor == null ? "" : cursor.getString(cursor
					.getColumnIndex("_id"));
		}
		
		public ZhanAdapter(Context context, Cursor c, boolean autoRequery) {
			super(context, c, autoRequery);
			layoutInflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		}
		
		private void setView(View view,Cursor cursor){
			TextView zhanText = (TextView) view;
			zhanText.setText(cursor.getString(cursor.getColumnIndex("_id")));
		}

		@Override
		public void bindView(View view, Context context, Cursor cursor) {
			setView(view,cursor);
		}

		@Override
		public View newView(Context context, Cursor cursor, ViewGroup parent) {
			View view = layoutInflater.inflate(R.layout.zhan_list, null);
			return view;
		}
	}

}
